import 'package:flutter/material.dart';

/// 手绘风格的图标定义
class HandDrawnIcons {
  // 手绘风格的情绪图标路径
  static const String moodVeryBad = 'lib/assets/icons/mood_very_bad.png';
  static const String moodBad = 'lib/assets/icons/mood_bad.png';
  static const String moodNeutral = 'lib/assets/icons/mood_neutral.png';
  static const String moodGood = 'lib/assets/icons/mood_good.png';
  static const String moodVeryGood = 'lib/assets/icons/mood_very_good.png';
  
  // 手绘风格的导航图标路径
  static const String navMood = 'lib/assets/icons/nav_mood.png';
  static const String navDiary = 'lib/assets/icons/nav_diary.png';
  static const String navStats = 'lib/assets/icons/nav_stats.png';
  static const String navProfile = 'lib/assets/icons/nav_profile.png';
  
  // 手绘风格的装饰元素
  static const String decorationCloud = 'lib/assets/decorations/cloud.png';
  static const String decorationSun = 'lib/assets/decorations/sun.png';
  static const String decorationFlower = 'lib/assets/decorations/flower.png';
  static const String decorationLeaf = 'lib/assets/decorations/leaf.png';
  
  // 获取情绪对应的图标路径
  static String getMoodIconPath(int moodType) {
    switch (moodType) {
      case 1:
        return moodVeryBad;
      case 2:
        return moodBad;
      case 3:
        return moodNeutral;
      case 4:
        return moodGood;
      case 5:
        return moodVeryGood;
      default:
        return moodNeutral;
    }
  }
}

/// 手绘风格的自定义绘制组件
class HandDrawnContainer extends StatelessWidget {
  final Widget child;
  final Color color;
  final double borderWidth;
  final Color borderColor;
  final double borderRadius;
  final EdgeInsetsGeometry padding;
  
  const HandDrawnContainer({
    Key? key,
    required this.child,
    this.color = Colors.white,
    this.borderWidth = 2.0,
    this.borderColor = Colors.black,
    this.borderRadius = 8.0,
    this.padding = const EdgeInsets.all(16.0),
  }) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: _HandDrawnRectPainter(
        color: color,
        borderWidth: borderWidth,
        borderColor: borderColor,
        borderRadius: borderRadius,
      ),
      child: Padding(
        padding: padding,
        child: child,
      ),
    );
  }
}

/// 手绘风格的矩形绘制器
class _HandDrawnRectPainter extends CustomPainter {
  final Color color;
  final double borderWidth;
  final Color borderColor;
  final double borderRadius;
  
  _HandDrawnRectPainter({
    required this.color,
    required this.borderWidth,
    required this.borderColor,
    required this.borderRadius,
  });
  
  @override
  void paint(Canvas canvas, Size size) {
    final rect = Rect.fromLTWH(0, 0, size.width, size.height);
    final rrect = RRect.fromRectAndRadius(
      rect,
      Radius.circular(borderRadius),
    );
    
    // 填充背景
    final fillPaint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;
    canvas.drawRRect(rrect, fillPaint);
    
    // 绘制手绘风格的边框
    final borderPaint = Paint()
      ..color = borderColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = borderWidth
      ..strokeCap = StrokeCap.round
      ..strokeJoin = StrokeJoin.round
      ..isAntiAlias = true;
    
    // 添加一些随机性使边框看起来像手绘的
    final path = Path();
    
    // 左上角到右上角
    path.moveTo(borderRadius, 0);
    _addWavyLine(path, borderRadius, 0, size.width - borderRadius, 0);
    
    // 右上角到右下角
    path.moveTo(size.width, borderRadius);
    _addWavyLine(path, size.width, borderRadius, size.width, size.height - borderRadius);
    
    // 右下角到左下角
    path.moveTo(size.width - borderRadius, size.height);
    _addWavyLine(path, size.width - borderRadius, size.height, borderRadius, size.height);
    
    // 左下角到左上角
    path.moveTo(0, size.height - borderRadius);
    _addWavyLine(path, 0, size.height - borderRadius, 0, borderRadius);
    
    // 绘制圆角
    _addWavyArc(path, borderRadius, borderRadius, 180, 270); // 左上角
    _addWavyArc(path, size.width - borderRadius, borderRadius, 270, 360); // 右上角
    _addWavyArc(path, size.width - borderRadius, size.height - borderRadius, 0, 90); // 右下角
    _addWavyArc(path, borderRadius, size.height - borderRadius, 90, 180); // 左下角
    
    canvas.drawPath(path, borderPaint);
  }
  
  // 添加波浪线
  void _addWavyLine(Path path, double startX, double startY, double endX, double endY) {
    final random = _PseudoRandom(startX * 1000 + startY * 100);
    final dx = endX - startX;
    final dy = endY - startY;
    final distance = (dx * dx + dy * dy).sqrt();
    final segments = (distance / 5).ceil();
    
    if (segments <= 1) {
      path.lineTo(endX, endY);
      return;
    }
    
    for (int i = 1; i <= segments; i++) {
      final t = i / segments;
      final x = startX + dx * t;
      final y = startY + dy * t;
      
      // 添加一些随机偏移使线条看起来不规则
      final offset = random.nextDouble() * 2 - 1;
      final perpX = -dy / distance * offset;
      final perpY = dx / distance * offset;
      
      path.lineTo(x + perpX, y + perpY);
    }
  }
  
  // 添加波浪弧
  void _addWavyArc(Path path, double centerX, double centerY, double startAngle, double endAngle) {
    final random = _PseudoRandom(centerX * 1000 + centerY * 100);
    final angleStep = 5;
    
    for (int angle = startAngle.toInt(); angle < endAngle.toInt(); angle += angleStep) {
      final radians = angle * 3.14159 / 180;
      final nextRadians = (angle + angleStep) * 3.14159 / 180;
      
      final x1 = centerX + borderRadius * cos(radians);
      final y1 = centerY + borderRadius * sin(radians);
      final x2 = centerX + borderRadius * cos(nextRadians);
      final y2 = centerY + borderRadius * sin(nextRadians);
      
      // 添加一些随机偏移
      final offset = random.nextDouble() * 1.5 - 0.75;
      final midX = (x1 + x2) / 2 + offset;
      final midY = (y1 + y2) / 2 + offset;
      
      path.moveTo(x1, y1);
      path.lineTo(midX, midY);
      path.lineTo(x2, y2);
    }
  }
  
  double cos(double radians) {
    return math.cos(radians);
  }
  
  double sin(double radians) {
    return math.sin(radians);
  }
  
  @override
  bool shouldRepaint(_HandDrawnRectPainter oldDelegate) {
    return oldDelegate.color != color ||
        oldDelegate.borderWidth != borderWidth ||
        oldDelegate.borderColor != borderColor ||
        oldDelegate.borderRadius != borderRadius;
  }
}

/// 伪随机数生成器，用于生成一致的"随机"效果
class _PseudoRandom {
  int _seed;
  
  _PseudoRandom(this._seed);
  
  double nextDouble() {
    _seed = (_seed * 1103515245 + 12345) & 0x7FFFFFFF;
    return _seed / 0x7FFFFFFF;
  }
}

// 导入数学库
import 'dart:math' as math; 